import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:juming_test/model/domain/domain_filter_item_model.dart';
import 'package:juming_test/model/domain/domain_filter_option_model.dart';

/// 两个输入框组件
class DomainFilterMultipleInput extends StatefulWidget {
  const DomainFilterMultipleInput({
    super.key,
    required this.model,
    required this.onInput1Completed,
    required this.onInput2Completed,
  });

  // 数据源
  final DomainFilterItemModel model;
  // 事件
  // 输入完成回调
  final Function(String) onInput1Completed;
  final Function(String) onInput2Completed;

  @override
  State<DomainFilterMultipleInput> createState() =>
      _DomainFilterMultipleInputState();
}

class _DomainFilterMultipleInputState extends State<DomainFilterMultipleInput> {
  // 输入框控制器1
  late TextEditingController _inputController1;
  // 输入框控制器2
  late TextEditingController _inputController2;
  // 焦点
  late FocusNode _focusNode1;
  // 焦点
  late FocusNode _focusNode2;

  @override
  void initState() {
    super.initState();
    _initInputController();
  }

  @override
  void didUpdateWidget(covariant DomainFilterMultipleInput oldWidget) {
    super.didUpdateWidget(oldWidget);
    _inputController1.text = widget.model.inputValue1;
    _inputController2.text = widget.model.inputValue2;
  }

  @override
  void dispose() {
    _inputController1.dispose();
    _inputController2.dispose();
    _focusNode1.dispose();
    _focusNode2.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Container(
            decoration: BoxDecoration(
              color: Colors.grey[200],
              borderRadius: BorderRadius.circular(4),
            ),
            child: TextField(
              controller: _inputController1,
              focusNode: _focusNode1,
              onEditingComplete: () {
                _focusNode1.unfocus();
                widget.onInput1Completed(_inputController1.text);
              },
              decoration: InputDecoration(
                hintText: '请输入',
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(horizontal: 12.w),
              ),
            ),
          ),
        ),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 8.w),
          child: Text(
            '—',
            style: TextStyle(fontSize: 16.sp, color: Colors.grey[600]),
          ),
        ),
        Expanded(
          child: Container(
            decoration: BoxDecoration(
              color: Colors.grey[200],
              borderRadius: BorderRadius.circular(4),
            ),
            child: TextField(
              controller: _inputController2,
              focusNode: _focusNode2,
              onEditingComplete: () {
                _focusNode2.unfocus();
                widget.onInput2Completed(_inputController2.text);
              },
              decoration: InputDecoration(
                hintText: '请输入',
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(horizontal: 12.w),
              ),
            ),
          ),
        ),
      ],
    );
  }

  // 初始化输入框控制器
  void _initInputController() {
    _inputController1 = TextEditingController(text: widget.model.inputValue1);
    _inputController2 = TextEditingController(text: widget.model.inputValue2);
    _focusNode1 = FocusNode();
    _focusNode2 = FocusNode();
  }
}
